<?php 
class smart_pager{
	private $conf=null;
	private $db=null;
	public  $total_num=0;
	private $total_page=0;
	private $page_id=0;
	//http://www.book.com/admin.php/admin/li?page_id=2
	//http://www.book.com/admin.php?m=admin&a=li&page_id=2
	public  $url_tpl=array(
			'path'=>'',
			'filename'=>'?page_id=%page_id%',
			'extension'=>'',
			);
	private  $url='';
	private $param=null;

	public  $query=null;
	private $pagesize=0;
	public  $template='%_first% %_up% %_down% %_last% %_info% %_lifter%';
	public 	$text=array(
			'first'=>'首页',
			'last' =>'尾页',
			'up'   =>'上一页',
			'down' =>'下一页',
			'info' =>'%page_id%/%total_page%|共条%total_num%记录,每页显示%pagesize%条',
			'lifter'=>"跳至：<input type='text' id='pagejump' value='%n%' />&nbsp;
						<input type='button' id='pagego' value='Go!' onclick='pl(\"pagejump\",\"%u%\");' />
			          ",
			);
	public $style=array(
		   'first'=>'pager_first',
		   'last' =>'pager_last',
		   'up'   =>'pager_up',
		   'down' =>'pager_down',	
		   'info' =>'pager_info',
		   'lifter'=>'pager_lifter',
		   );

	public function __construct($sql,$dblink,$pagesize=20){
		$this->conf=Conf::getIns();
		$this->query=$sql;
		$this->db=$dblink;
		$this->pagesize=$pagesize;
		$sql=preg_replace("/([\w\W]*?select)([\w\W]*?)(from[\w\W]*?)/i","$1 count(*) $3",$sql);
		$this->total_num=$this->db->get_field($sql);
		isset($_GET['page_id'])?'':$_GET['page_id']=1;
		$this->page_id=$_GET['page_id']+0;
		$this->page_id=($this->page_id<=0)?1:$this->page_id;
		$this->total_page=ceil($this->total_num/$this->pagesize);
		$this->page_id=($this->page_id>$this->total_page)?$this->total_page:$this->page_id;
		$this->query.=' limit '.($this->page_id-1)*$this->pagesize.','.$this->pagesize;
		//附加m a
		if(!$conf->ENABLE_PATH_INFO) $this->url_tpl['filename'].="&m={$_GET['m']}&a={$_GET['a']}";
		//$this->auto_get();
		//$this->formate_url();
	}
	//$t &cat_id=%cat_id% cat_id 100 多次调用
	public function address($t,$key,$value){
		$this->url_tpl['filename'].=$t;
		$this->url_tpl['filename']=str_replace('%'.$key.'%', $value, $this->url_tpl['filename']);
	}
	/*
	//自动处理分页参数
	private function auto_get(){
		$p_url=$_SERVER['QUERY_STRING'];
		parse_str($p_url,$arr);
		if(array_key_exists('page_id',$arr)){
			unset($arr['page_id']);
		}
		$this->param=$arr;
	}
	//格式化文件地址
	private function formate_url(){
		if (is_array($this->param) && count($this->param)) {	
			foreach ($this->param as $key => $value) {
				$this->url_tpl['filename']=str_replace('%'.$key.'%', $value, $this->url_tpl['filename']);
			}
		}
	}
	*/
	private function add_page_id($id=null){
		if(!is_null($id)){
			$f=str_replace('%page_id%', $id, $this->url_tpl['filename']);
			return $this->url_tpl['path'].$f.$this->url_tpl['extension'];
		}else{
			return $this->url_tpl['path'].$this->url_tpl['filename'].$this->url_tpl['extension'];
		}	
	}
	public function __tostring(){
		$str=$this->template;
		$array=array('_first','_last','_up','_down','_info','_lifter');
		foreach ($array as $key => $value) {
			$str=str_replace("%{$value}%", $this->$value(), $str);
		}
		return $str;
	}
	public function _first(){
		$str='';
		if ($this->page_id==1) {
			$str="<span class='{$this->style['first']}'>{$this->text['first']}</span>";
		} else {
			$address=$this->add_page_id('1');
			$str="<span class='{$this->style['first']}'><a href='{$address}'>{$this->text['first']}</a></span>";
		}
		return $str;
	}
	public function _last(){
		$str='';
		if ($this->page_id==$this->total_page) {
			$str="<span class='{$this->style['last']}'>{$this->text['last']}</span>";
		} else {
			$address=$this->add_page_id($this->total_page);
			$str="<span class='{$this->style['last']}'><a href='{$address}'>{$this->text['last']}</a></span>";
		}
		return $str;
	}
	public function _up(){
		$str='';
		if ($this->page_id==1) {
			$str="<span class='{$this->style['up']}'>{$this->text['up']}</span>";
		} else {
			$n=$this->page_id-1;
			$n=$n<1?1:$n;
			$address=$this->add_page_id($n);
			$str="<span class='{$this->style['up']}'><a href='{$address}'>{$this->text['up']}</a></span>";
		}
		return $str;
	}
	public function _down(){
		$str='';
		if ($this->page_id==$this->total_page) {
			$str="<span class='{$this->style['down']}'>{$this->text['down']}</span>";
		} else {
			$n=$this->page_id+1;
			$n=$n>$this->total_page?$this->total_page:$n;
			$address=$this->add_page_id($n);
			$str="<span class='{$this->style['down']}'><a href='{$address}'>{$this->text['down']}</a></span>";
		}
		return $str;
	}
	public function _info(){
		$str='';
		$array=array('%page_id%','%total_page%','%total_num%','%pagesize%');
		foreach ($array as $key => $value) {
			$replace=trim($value,'%');
			$this->text['info']=str_replace($value, $this->$replace, $this->text['info']);
		}
		$str="<span class='{$this->style['info']}'>{$this->text['info']}</span>";
		return $str;
	}
	public function _lifter(){
		$str='';
		$array=array('%n%','%u%');
		$n=$this->page_id+1;
		$n=($n>$this->total_page)?$this->total_page:$n;
		$u=$this->add_page_id(null);
		foreach ($array as $key => $value) {
			$replace=trim($value,'%');
			$this->text['lifter']=str_replace($value, $$replace, $this->text['lifter']);
		}
		$str="<span class='{$this->style['lifter']}'>{$this->text['lifter']}</span>";
		return $str;
	}
//
}